package com.ykjndz.bt;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

/**
 * @描述: TODO 警报按钮
 * @作者: qius
 */
public class AlarmButton extends Button {
    private AlarmLongClick mAlarmLongClick;

    public AlarmButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public AlarmButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AlarmButton(Context context) {
        super(context);
    }

    public void setOnAlarmLongClick(AlarmLongClick alarmLongClick) {
        mAlarmLongClick = alarmLongClick;
        super.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // System.out.println(arg1.getEventTime() -
                    // arg1.getDownTime());
                    break;
                case MotionEvent.ACTION_UP:
                    long time = (arg1.getEventTime() - arg1.getDownTime()) / 1000;
                    System.out.println(time);
                    if (time < 2) {
                        if (mAlarmLongClick != null) {
                            mAlarmLongClick.onOneClick();
                        }
                    } else if (time == 2 || time == 3 || time == 4) {
                        if (mAlarmLongClick != null) {
                            mAlarmLongClick.onThreeClick();
                        }
                    } else if (time > 7) {
                        if (mAlarmLongClick != null) {
                            mAlarmLongClick.onEightMoreClick();
                        }
                    }
                    break;
                default:
                    break;
                }
                return false;
            }
        });
    }

    public interface AlarmLongClick {
        /**
         * 轻触（1秒内）
         */
        void onOneClick();

        /**
         * 3秒~4秒
         */
        void onThreeClick();

        /**
         * 8秒以上
         */
        void onEightMoreClick();
    }

}
